//
//  MyItems.swift
//  Fancyswap
//
//  Created by Jonathan Muschalle on 2015-04-12.
//  Copyright (c) 2015 Jonathan Muschalle. All rights reserved.
//

import UIKit

struct MyItem {
    var name = ""
    var id = 0
    var brand = ""
    var value = 0
    var size = ""
    var status = 0
    var image = ""
    var imageObject:UIImage = UIImage()
}

class MyItems {

    private var items = [MyItem]()
    let MY_ITEMS_STATUS_SUCCESS = 1
    let STATUS_CANCELLED = 4;
    let STATUS_ORDERED = 3;
    let STATUS_WAIT_APPROVAL = 2;
    let STATUS_ACTIVE = 1;
    let STATUS_INACTIVE = 0;
    
    init() {
        items = [MyItem]()
    }
    
    func hasIndex(index:Int) -> Bool {
        if(items.count > index) {
            return true
        } else {
            return false
        }
    }
    
    func getTotalItems() -> Int {
        return items.count
    }
   
    func append(item: MyItem) {
        items.append(item)
    }
    
    func loadJson(jsonItems: [NSDictionary]) {
        for jsonItem in jsonItems {
            var item :MyItem = MyItem()
            item.id = jsonItem["id"] as Int!
            item.name = jsonItem["name"] as String!
            item.brand = jsonItem["brand"] as String!
            item.value = jsonItem["value"] as Int!
            item.size = jsonItem["size"] as String!
            item.status = jsonItem["status"] as Int!
            
            item.image = jsonItem["image"] as String!
            let url = NSURL(string: "http://api.fancyswap.com/images/data/thumbs/\(item.image)")
            let imageData = NSData(contentsOfURL: url!)
            let image = UIImage(data: imageData!)!
            item.imageObject = image
            items.append(item)
        }
    }
    
    func getCellByIndex(index: NSIndexPath, cell: MyItemCell) -> MyItemCell {
        
        cell.itemName.text = items[index.row].name
        cell.itemBrand.text = items[index.row].brand
        cell.itemCoins.text = "\(items[index.row].value)"
        
        cell.itemImage.image = items[index.row].imageObject
        switch items[index.row].status {
        case STATUS_CANCELLED:
            cell.itemStatus.text = "Annulé"
            cell.itemStatus.textColor = UIColor.redColor()
        case STATUS_ORDERED:
            cell.itemStatus.text = "Commandé"
            cell.itemStatus.textColor = UIColor.blueColor()
        case STATUS_WAIT_APPROVAL:
            cell.itemStatus.text = "En attente"
            cell.itemStatus.textColor = UIColor.orangeColor()
        case STATUS_ACTIVE:
            cell.itemStatus.text = "Approuvé"
            cell.itemStatus.textColor = UIColor.greenColor()
        case STATUS_INACTIVE:
            cell.itemStatus.text = "Refusé"
            cell.itemStatus.textColor = UIColor.redColor()
        default:
            cell.itemStatus.text = ""
        }
        
        return cell
    }
    
    func loadItems(controller: MyItemsController) {
        items = []
        controller.refreshControl?.beginRefreshing()
//        fancy_token = "b53f8e9c2960eb726ac4f7acf190123c280e6240c"
        Manager.sharedInstance.session.configuration.HTTPAdditionalHeaders = ["Authorization":fancy_token]
        request(
            .GET,
            "http://api.fancyswap.com/rest/1.0/items/me")
            .responseJSON {
                (request, data, JSON_REPONSE, error) in
                let jsonResult: AnyObject = JSON_REPONSE!
                
                if let status = jsonResult["status"] as? Int {
                    if(status == self.MY_ITEMS_STATUS_SUCCESS) {
                        var items = jsonResult["items"] as? [NSDictionary]
                        myItems.loadJson(items!)
                        controller.tableView.reloadData()
                        controller.refreshControl?.endRefreshing()
                    }
                }
                
        }
    }
    
}

var myItems = MyItems()

